<?php
$MYSQL_URL="esgwebmysql.win.ad.jhu.edu";
$MYSQL_USER="aidjhu";
$MYSQL_PASSWORD="bn4atyara6zxjfyt";
$MYSQL_DATABASE="aidjhu";


function get_connection()
{
	global $MYSQL_URL, $MYSQL_USER, $MYSQL_PASSWORD, $MYSQL_DATABASE;
	$con=mysql_connect($MYSQL_URL,$MYSQL_USER,$MYSQL_PASSWORD);
	if(!$con)
		die('Could not connect: ' . mysql_error());
	mysql_select_db($MYSQL_DATABASE,$con);
	return $con;
}

function close_connection($con)
{
	mysql_close($con);
}

function create_new_event_table($event_id,$seatprice,$con)
{
	$query="CREATE TABLE `hh_seat_allocate_$event_id`(`seat_no` INT NOT NULL AUTO_INCREMENT,
													  `seat_name` VARCHAR(45) NULL,
													  `row_no` VARCHAR(45) NULL,
													  `section` VARCHAR(45) NULL,
													  `price` INT NULL,
													  `user_id` BIGINT NULL,
													  `status` INT NULL,
													  PRIMARY KEY (`seat_no`));";
	if(!mysql_query($query,$con))
		die('Error in query: ' . mysql_error());
	populate_event_table($event_id,$seatprice,$con);
}

function populate_event_table_xml($event_id,$xmlfile,$con)
{
    $seatchart = simplexml_load_file($xmlfile);	
	mysql_query("BEGIN",$con);
	foreach ($seatchart as $section1):	    
	    foreach ($section1 as $row):
		     echo $row['name'];
		     foreach ($row as $seat):
			      $seatname = $seat['name'];
				  $seatprice = $seat->price;				  
			      //echo "<li><div class='Seat Number'>",$seatname,"</div><div class='Price'>",$seatprice,"</div></li>";
				  insert_into_event($event_id, $seatname, $row['name'], 'general', $seatprice, $con);
			 endforeach;
		endforeach;
    endforeach;
	mysql_query("COMMIT",$con);
	/*mysql_query("BEGIN",$con);
	for($row='A';$row<='J';$row++)
		for($i=1;$i<=20;$i++)
			insert_into_event($event_id, $row.$i, $row, 'general', $seatprice, $con);
	mysql_query("COMMIT",$con);*/
}
function create_new_xml_event_table($event_id,$xmlfile,$con)
{
	$query="CREATE TABLE `hh_seat_allocate_$event_id`(`seat_no` INT NOT NULL AUTO_INCREMENT,
													  `seat_name` VARCHAR(45) NULL,
													  `row_no` VARCHAR(45) NULL,
													  `section` VARCHAR(45) NULL,
													  `price` INT NULL,
													  `user_id` BIGINT NULL,
													  `status` INT NULL,
													  PRIMARY KEY (`seat_no`));";
	if(!mysql_query($query,$con))
		die('Error in query: ' . mysql_error());
	populate_event_table_xml($event_id,$xmlfile,$con);
}

function insert_into_event($event_id, $seat_name, $row_no, $section, $price, $con)
{
	$query="INSERT INTO `hh_seat_allocate_$event_id`(`seat_name`,`row_no`,`section`,`price`,`user_id`,`status`) VALUES('$seat_name','$row_no','$section',$price,0,0)";
	if(!mysql_query($query,$con))
		die('Error in query: ' . mysql_error());
}

function populate_event_table($event_id,$seatprice,$con)
{
	/* $seatchart = simplexml_load_file('seatchart.xml');
	mysql_query("BEGIN",$con);
	foreach ($seatchart as $section1):
		foreach ($section1 as $row):
			foreach ($row as $seat):
				$seatname = $seat['name'];
				$seatprice = $seat->price;
				insert_into_event($event_id, $seatname, $row['name'], $seatchart->section1[0]['name'], $seatprice, $con);
			endforeach;
		endforeach;
	endforeach;
	mysql_query("COMMIT",$con); */
	
	mysql_query("BEGIN",$con);
	for($row='A';$row<='J';$row++)
		for($i=1;$i<=20;$i++)
			insert_into_event($event_id, $row.$i, $row, 'general', $seatprice, $con);
	mysql_query("COMMIT",$con);
}

function get_seats($event_id,$con)
{
	$query="SELECT * FROM `hh_seat_allocate_$event_id`";
	if($result=mysql_query($query,$con))
	{
		while($row=mysql_fetch_array($result))
		{
			$seat[$row['seat_no']]['seat_name']=$row['seat_name'];
			$seat[$row['seat_no']]['row_no']=$row['row_no'];
			$seat[$row['seat_no']]['section']=$row['section'];
			$seat[$row['seat_no']]['price']=$row['price'];
			$seat[$row['seat_no']]['user_id']=$row['user_id'];
		}
		return $seat;
	}
	else
	{
		die('Error in query: ' . mysql_error());
	}
}

function book_seat($event_id, $seat_no, $user_id, $con)
{
	$query="UPDATE `hh_seat_allocate_$event_id` SET `user_id`=$user_id, `status`=1 WHERE `seat_no`=$seat_no";
	if(!mysql_query($query,$con))
		die('Test Error in query: ' . mysql_error());
}
function findfriends ($eventid, $con, $fb) {        
	    $event_id = $eventid;
	    $userfriends=$fb->api('/me/friends');
	    
	    //echo $con."<br/>";
	    //echo $userfriends->id[0];
	    $query = "select seat_name, row_no, section, user_id from hh_seat_allocate_".$event_id;	
	    //echo $query;
	    //echo "<br/>";
	    $attendees=mysql_query($query, $con);
	    if(! $attendees )
        {
           die('Could not get data: ' . mysql_error());
        }
	    $i=0;
	    while($row = mysql_fetch_array($attendees, MYSQL_ASSOC))
        {
	        $seatname[$i]=$row['seat_name'];
		    $rowno[$i]=$row['row_no'];
		    $sect[$i]=$row['section'];
		    $userid[$i]=$row['user_id'];
		    $i=$i+1;
            /*echo "Seat Name :{$row['seat_name']}  <br> ".
             "Row No : {$row['row_no']} <br> ".
             "Section : {$row['section']} <br> ".
			 "User ID : {$row['user_id']} <br> ".
             "--------------------------------<br>";*/
			 
        } 
        //echo "Fetched data successfully ".$i."<br/>";
        $k=0;
	    foreach ($userfriends as $friends) {
            foreach ($friends as $friend) {
            //do something with the friend, but you only have id and name
		       if (isset($friend['id'])) { 
		           $id1 = $friend['id'];
                   $name1 = $friend['name'];
		           // echo $id1 . $name1 . "<br/>";
		           for ($j=0;$j<$i; $j++){             
		               //$query=$query.$id
			           if ($id1==$userid[$j]) {
                           $data->ret_seatname[$k]=$seatname[$j];
		                   $data->ret_rowno[$k]=$rowno[$j];
		                   $data->ret_sect[$k]=$sect[$j];
		                   $data->ret_userid[$k]=$userid[$j];
                           $k=$k+1;				
			           } 		     
		           }
		       }		 
            }
	    }
	
	    /*for ($l=0;$l<$k;$l++) {
	        echo $data->ret_seatname[$l]." ".$data->ret_rowno[$l]." ".$data->ret_sect[$l]." ".$data->ret_userid[$l]."<br/>";
	    }*/
	
	    mysql_free_result($attendees);
		if (isset($data)) {
	       return $data;
		} else {
		   return null;
		}
	}
	
	function countData($data1) {
	    if ($data1 == null) {
		    return 0;
		}
	    foreach ($data1 as $data){
	       $k=0;
		   foreach ($data as $key) {
	          $k=$k+1;        
		   }
	    }
		return $k;
	}
	
	function isFriendSeat($friendlist, $friendID) {
	    $k = countData($friendlist);
        for ($l=0; $l<$k;$l++){
	         if ($friendID==$friendlist->ret_userid[$l]) {
                 return true;			 
			 }
			 //echo $data->ret_seatname[$l]." ".$data->ret_rowno[$l]." ".$data->ret_sect[$l]." ".$data->ret_userid[$l]."<br/>";
	  
	    }	
		return false;
	}

//Check if a user is logged in or not

require_once("php-sdk/facebook.php");

$config = array();
$config['appId'] = '658293284188810';
$config['secret'] = 'a54eac52b0c1cb456ae2b9354976be9d';

$fb = new Facebook($config);

$user = $fb->getUser();

if ($user) {
	try {
		$user_profile = $fb->api('/me');
	} catch (FacebookApiException $e) {
		error_log($e);
		$user = null;
	}
}
else{
	$loginUrl = $fb->getLoginUrl(array('scope' => 'create_event, publish_actions, publish_stream, user_events, rsvp_event'));
	echo "<div style='text-align:center'>Please, <a href=" . $loginUrl . ">SignIn</a> using FB.</div>";
	die();
}


?>